/*
 邀请面试对话框
 @绑定查看联系方式
 */
$(function(){
	$.extend({
		//企业将个人纳入人才库
		insertPersonnelLibrary: function(settings){
			var setvar = {
				className: "",
				crsf: "",
				url: "",
				personnelLibraryUrl: "",
				delPersonnelLibrary: "",
				distributionJob: "",
				postJobUrl: "",
				resumeId: "",
				userName: "",
				sign: "",
				callBack: function(){}
			};
			$.extend(setvar, settings);


			toInterView = layer.open({
				type: 1,
				title: "<b style='color:#2e8ded'>请您给[<i style='color: #21a74f;'>" + setvar.userName + "</i>]分配一个合适的岗位</b>",
				skin: 'layer-interview',
				area: ['840px', '638px'],
				resize: false,
				move: false,
				btn: false,
				content: "<div id='toInterViewDiv'></div>",
				success: function(){
					$.post(setvar.url, {
						'_token': setvar.crsf,
						'resumeId': setvar.resumeId
					}, function(data){
						switch(data.status){
							case 2:
								//layer.msg('亲，您还未发布工作岗位！', {icon: 6});
								setvar.callBack(data.status);
								break;
							case 1:
								jobTitle = data.jobTitle;
								html = '';
								//option
								html += "<div class='insertPersonnelLibrary'>";
								html += "<form action='#' method='post' name='insertPersonnelLibraryForm' id='insertPersonnelLibraryForm' >";//company/interviewnote
								html += "<input type='hidden' name='_token' value='" + setvar.crsf + "'>";
								html += "<input type='hidden' name='resumeId' value='" + setvar.resumeId + "'>";

								html += "<ul class='checkList'>";
								html += "<li class='first titleButtom bloder'>序号</li>";
								html += "<li class='second titleButtom bloder'>岗位名称</li>";
								html += "<li class='third titleButtom bloder'>职位薪资</li>";
								html += "<li class='four titleButtom bloder'>工作地区</li>";
								html += "<li class='five titleButtom bloder'>更新时间</li>";
								html += "<li class='six titleButtom bloder'></li>";
								html += "</ul>";
								//显示已纳入人才
								html += "<div id='havePersonnelShow'></div>";
								haveHtml = "";

								if(jobTitle.length == 0){
									html += "<div class='haveNoJob'>";
									html += "<p class='first-tip'>您还没有发布的职位</p>";
									html += "<p class='second-tip'>发布职位，320万人才给您投递简历</p>";
									html += "<a href=" + setvar.postJobUrl + " class='button' target='_blank'>立即发布职位</a>";
									html += "</div>";
								}

								$.each(jobTitle, function(key, value){
									//已纳入人才库
									html += "<ul class='list dropHaveColor' id='list" + (key + 1) + "'>";
									html += "<li class='first'><input name='d_job' value='" + value.id + "' type='radio' ></li>";
									html += "<li class='second'>" + value.job_title + "</li>";
									html += "<li class='third'>" + value.salary_above + "</li>";
									html += "<li class='four'>" + value.location + "</li>";
									html += "<li class='five'>" + value.updatetime + "</li>";
									html += "<li class='six'></li>";
									html += "</ul>";

									//if(value.personnel_is_have_insert != null){
									//    if(value.personnel_is_have_insert.have == 1){
									//        haveHtml += "<ul class='list HaveColor' id='list"+(key+1)+"'>";
									//        haveHtml += "<li class='first'><input name='' value='1' type='radio'></li>";
									//        haveHtml += "<li class='second'>"+value.job_title+"</li>";
									//        haveHtml += "<li class='third'>"+value.salary_above+"</li>";
									//
									//        haveHtml += "<li class='four'>"+value.location+"</li>";
									//        haveHtml += "<li class='five'>"+value.updatetime+"</li>";
									//        haveHtml += "<li class='six'><span class='delete'><em>取消分配</em><input type='hidden' value='"+value.id+"'><input type='hidden' value='"+(key+1)+"'></span></li>";
									//        haveHtml += "</ul>";
									//    }else{
									//        html += "<ul class='list dropHaveColor' id='list"+(key+1)+"'>";
									//        html += "<li class='first'>"+(key+1)+"</li>";
									//        html += "<li class='second'>"+value.job_title+"</li>";
									//        html += "<li class='third'>"+ value.salary_above+"</li>";
									//
									//        html += "<li class='four'>"+value.location+"</li>";
									//        html += "<li class='five'>"+value.updatetime+"</li>";
									//        html += "<li class='six'><span class='add'><em>分配该岗位</em><input type='hidden' value='"+value.id+"'><input type='hidden' value='"+(key+1)+"'></span></li>";
									//        html += "</ul>";
									//    }
									//}else{
									//    html += "<ul class='list dropHaveColor' id='list"+(key+1)+"'>";
									//    html += "<li class='first'>"+(key+1)+"</li>";
									//    html += "<li class='second'>"+value.job_title+"</li>";
									//    html += "<li class='third'>"+value.salary_above+"</li>";
									//
									//    html += "<li class='four'>"+value.location+"</li>";
									//    html += "<li class='five'>"+value.updatetime+"</li>";
									//    html += "<li class='six'><span class='add'><em>分配该岗位</em><input type='hidden' value='"+value.id+"'><input type='hidden' value='"+(key+1)+"'></span></li>";
									//    html += "</ul>";
									//}
								});
								html += "</form>";
								html += "</div>";
								//确定按钮
								html += "<div class='confirm'>";
								html += "<input type='button' value='确 定'>";
								html += "</div>";
								$('#toInterViewDiv').html(html);

								//追加到显示纳入人才div
								$("#havePersonnelShow").html(haveHtml);
								//确定关闭按钮
								$(".confirm").find("input").eq(0).click(function(){
									layer.close(toInterView);
								});

								//获取岗位信息
								if(data.resumeFavorite != null){
									$("#insertPersonnelLibraryForm").find('ul.list').find("input[name='d_job'][value='" + data.resumeFavorite.job_id + "']").attr('checked',true)
										.parents('ul.list').addClass('HaveColor').siblings().removeClass('HaveColor');
								}
								//分配岗位
								$("#insertPersonnelLibraryForm").find('ul.list').each(function(){
									$(this).click(function(){
										if(setvar.sign){
											setvar.sign = false;
											$(this).find("input[name='d_job']").attr('checked', true);
											$(this).addClass('HaveColor').siblings().removeClass('HaveColor');
											job_id = $(this).find("input[name='d_job']").val();
											//分配岗位
											$.post(setvar.distributionJob + "?resume_id=" + setvar.resumeId + "&job_id=" + job_id, {
												'_token': setvar.crsf
											}, function(data){
												if(data.status == 0){
													layer.msg(data.msg, {icon: 6, time: 1500});
													setvar.sign = true;
												}else{
													layer.msg(data.msg, {icon: 5});
													setvar.sign = true;
												}
											});
										}
									});
								});

								$(".list").find(".six").each(function(){
									$(this).find(".add").eq(0).click(function(){
										job_id = $(this).find("input").eq(0).val();
										thisId = $(this);
										$.post(setvar.personnelLibraryUrl + "?resume_id=" + setvar.resumeId + "&job_id=" + job_id, {
											'_token': setvar.crsf
										}, function(data){
											if(data.status == 0){
												switch(thisId.attr("class")){
													case "add":
														$(".list").find(".six").find(".delete").find("em").html("分配该岗位");
														$(".list").find(".six").find(".delete").attr("class", "add");
														thisUiId = thisId.find("input").eq(1).val();
														thisId.attr("class", 'delete');
														thisId.find("em").html("取消分配");
														$(".list").attr("class", "list dropHaveColor")
														$("#list" + thisUiId).attr("class", "list HaveColor");
														layer.msg(data.msg, {icon: 6});
														break;
													case "delete":
														$.post(setvar.delPersonnelLibrary + "?resume_id=" + setvar.resumeId + "&job_id=" + job_id, {
															'_token': setvar.crsf
														}, function(data){
															if(data.status == 0){
																thisId.attr("class", 'add');
																thisId.find("em").html("分配该岗位");
																thisUiId = thisId.find("input").eq(1).val();
																$(".list").attr("class", "list dropHaveColor");
																layer.msg(data.msg, {icon: 6});
																return true;
															}else{
																layer.msg(data.msg, {icon: 5});
																return false;
															}
														});
														break;
												}
												return true;
											}else{
												layer.msg(data.msg, {icon: 5});
												return false;
											}
										});
									});
								});

								//取消分配
								$(".list").find(".six").each(function(){
									$(this).find(".delete").eq(0).click(function(){
										alert(1111)
										job_id = $(this).find("input").eq(0).val();
										thisId = $(this);
										$.post(setvar.delPersonnelLibrary + "?resume_id=" + setvar.resumeId + "&job_id=" + job_id, {
											'_token': setvar.crsf
										}, function(data){
											if(data.status == 0){
												switch(thisId.attr("class")){
													case "delete":
														$(".list").find(".six").find(".delete").find("em").html("分配该岗位");
														$(".list").find(".six").find(".delete").attr("class", "add");
														thisUiId = thisId.find("input").eq(1).val();
														$(".list").attr("class", "list dropHaveColor")
														layer.msg(data.msg, {icon: 6});
														break;
													case "add":
														$.post(setvar.personnelLibraryUrl + "?resume_id=" + setvar.resumeId + "&job_id=" + job_id, {
															'_token': setvar.crsf
														}, function(data){
															if(data.status == 0){
																$(".list").find(".six").find(".delete").find("em").html("分配该岗位");
																$(".list").find(".six").find(".delete").attr("class", "add");
																thisUiId = thisId.find("input").eq(1).val();
																thisId.attr("class", 'delete');
																thisId.find("em").html("取消分配");
																$(".list").attr("class", "list dropHaveColor")
																$("#list" + thisUiId).attr("class", "list HaveColor");
																layer.msg(data.msg, {icon: 6});
																return true;
															}else{
																layer.msg(data.msg, {icon: 5});
																return false;
															}
														});
														break;
												}
												return true;
											}else{
												layer.msg(data.msg, {icon: 5});
												return false;
											}
										});
									});
								});
								break;
						}
					});
				}
			});
		}
	});
});
